package boun.center;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class BusAdapter extends BaseAdapter {
	ArrayList<String> schedule;
	private Context context;
	private String route;
	public BusAdapter(Context c,int dir, int dayType,String route) throws IOException, ParseException, ClassNotFoundException{
		BusSchedule.DayType type = BusSchedule.DayType.WEEK_DAY;
		BusSchedule.Direction direction = BusSchedule.Direction.FROM_LOCATION1_TO_LOCATION2;
		switch(dayType){
		case 0:
			type = BusSchedule.DayType.WEEK_DAY;
			break;
		case 1:
			type = BusSchedule.DayType.WEEK_END;
			break;
		case 2:
			type = BusSchedule.DayType.HOLIDAY;
			break;
		}
		switch(dir){
		case 0:
			direction = BusSchedule.Direction.FROM_LOCATION1_TO_LOCATION2;
			break;
		case 1:
			direction = BusSchedule.Direction.FROM_LOCATION2_TO_LOCATION1;
			break;
		}
		context = c;
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("route_id", route);
		map.put("route_dir",direction);
		map.put("day_type", type);
		ArrayList<String> tempSchedule = SocketUtil.fetchSchedule(map);
		if(tempSchedule != null)
			schedule = tempSchedule;
	}
	@Override
	public int getCount() {
		if(schedule != null)
			return schedule.size();
		else return 0;
	}

	@Override
	public Object getItem(int position) {
		return schedule.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		System.out.println("bassssssssssss");
		if(convertView == null){
			LayoutInflater inflater	= LayoutInflater.from(context);
			convertView = inflater.inflate(R.layout.cmpe_list_item,parent,false);
		}
		TextView tv = (TextView) convertView.findViewById(R.id.cmpe_subject);
		System.out.println("GETVIEW" + schedule.get(position));
		tv.setText(schedule.get(position));
		return convertView;
	}

}
